package middleware

import (
	"MBTIx/auth"
	"net/http"

	"github.com/gin-gonic/gin"
)

func JWTAuthMiddleware() gin.HandlerFunc {  
	return func(c *gin.Context) {
		authHeader := c.Request.Header.Get("X-Access-Token")
		if authHeader == "" {
			c.JSON(http.StatusOK, gin.H{
				"code": 2001,
				"msg":  "请求头中auth为空",
			})
			c.Abort()
			return
		}
		if user, err := auth.Parse(authHeader); err != nil {
			c.JSON(http.StatusOK,gin.H{
				"code":2003,
				"msg":"错误",
			})
			c.Abort()
		}else{
			c.Set("username",user.Name)
		}
	}
}
